/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: col</p>
 */
package com.googlecode.cswish.security.type;


/**
 * a structure that describles what user can operate to this path
 *
 * sub path is larger than alias path
 *
 * @author Jerry.Feng Date: 2005-7-4 15:35:00
 * @version $id: $
 */
public class Path {
	String path;

	RuleSetOr condition;

	private Path(){};

	public Path(String path, RuleSetOr condition) {
		if (path == null) throw new NullPointerException("path can't be null");

		this.path = path;
		if (condition == null) {
			this.condition = RuleSetOr.TRUE;
		} else {
			this.condition = condition;
		}
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public RuleSetOr getCondition() {
		return condition;
	}

	public void setCondition(RuleSetOr condition) {
		this.condition = condition;
	}

	public boolean equals(Object anObject) {
		if (this == anObject) {
			return true;
		}

		if (anObject instanceof Path) {
			Path another = (Path)anObject;

			return path.equals(another.getPath());
		} else {
			return false;
		}
	}

	/**
	 * Notice: only -1, 1 is the right compare result
	 * 0 means the two paths equal or the two paths can't be compared.
	 *
	 * @param another
	 * @return
	 */
	public int compare(Path another) {
		if (this.path.startsWith(another.getPath())) {
			// it means this.path is the sub path
			// for example:
			// this: /abc/def.do; another: /abc/
			return 1;
		} else if (another.getPath().startsWith(this.path)) {
			return -1;
		}
		return 0;
	}
}